package cn.kmia.hrm.action.basicData;

import cn.kmia.hrm.pojo.PostSignInfo;
import cn.kmia.hrm.pojo.system.User;
import cn.kmia.hrm.service.basicData.PostSignInfoService;
import cn.kmia.hrm.util.CommonSet;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Scope;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

/**
 * @program: hrm
 * @description: 岗位视线情况操作
 * @author: 4K
 * @create: 2018-07-19 16:29
 **/
@IocBean
@At("/postSignInfo")
public class PostSignInfoAction {

    private static final Log log = Logs.get();

    @Inject
    protected PostSignInfoService postSignInfoService;

    /**
     * 新增
     *
     * @param postSignInfo 岗位视线情况
     * @param user     操作者
     * @return
     */
    @At("/add")
    @Ok("json:full")
    @RequiresPermissions("postSignInfo:add")
    public Object add(@Param("..") PostSignInfo postSignInfo, @Attr(scope = Scope.SESSION, value = "user") User user) {
        NutMap nm = new NutMap();
        postSignInfo = postSignInfoService.add(postSignInfo, user.getUser_idcard());
        if (postSignInfo == null) {
            nm.setv("success", false).setv("errorMsg", "新增失败!");
        } else {
            nm.setv("success", true).setv("obj", postSignInfo);
        }
        return nm;
    }

    /**
     * 更新
     *
     * @param postSignInfo 岗位视线情况
     * @param user
     * @return
     */
    @At("/update")
    @Ok("json:full")
    @RequiresPermissions("postSignInfo:update")
    public Object update(@Param("..") PostSignInfo postSignInfo, @Attr(scope = Scope.SESSION, value = "user") User user) {
        NutMap re = new NutMap();
        try {
            if (postSignInfoService.update(postSignInfo, user.getUser_idcard())) {
                re.setv("success", true);
            } else {
                re.setv("success", false);
            }
        } catch (Exception e) {
            re.setv("success", false);
            log.error("更新异常", e);
        }
        return re;
    }

    /**
     * 删除
     *
     * @param ids int[]
     * @return
     */
    @At("/del")
    @Ok("json:full")
    @RequiresPermissions("postSignInfo:del")
    public Object del(@Param("ids") int[] ids) {
        NutMap re = new NutMap();
        try {
            if (ids != null) {
                re = postSignInfoService.del(ids);
            } else {
                re.setv("success", false);
            }
        } catch (Exception e) {
            re.setv("success", false);
            log.error("删除异常", e);
        }
        return re;
    }

    /**
     * 分页显示
     *
     * @param pager 分页
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("postSignInfo:queryByPage")
    public Object queryByPage(@Param("..") Pager pager, @Param("name") String name) {
        NutMap nm = new NutMap();
        QueryResult qr = postSignInfoService.queryByPage(name, pager);
        nm.setv("rows", qr.getList());
        nm.setv("total", qr.getPager().getRecordCount());
        return nm;
    }

    /**
     * 查询
     *
     * @return
     */
    @At("/query")
    @Ok("json:full")
    public Object query() {
        return CommonSet.PostSignInfo;
    }

    /**
     * 更新岗位视线情况
     *
     * @return
     */
    @At("/updatePostSignInfo")
    @Ok("json:full")
    public Object updatePostSignInfo() {
        NutMap nm = new NutMap();
        try{
            postSignInfoService.updatePostSignInfo();
            nm.setv("success",true);
        }catch (Exception e){
            nm.setv("success",false);
        }
        return nm;
    }

}
